package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.util.ValidateCodeUtils;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/userInfo")


public class UserInfoController extends BaseController {

    @Reference
    private UserInfoService userInfoService;

    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone, HttpServletRequest request) {
        //产生一个随机验证码
        String code = ValidateCodeUtils.generateValidateCode4String(6);

        //将验证码存入session   用来判断验证码是否正确
        request.getSession().setAttribute("CODE", code);


        //使用阿里短信发给指定的手机号

        return Result.ok(code);

    }

    @RequestMapping("/register")

    //post请求体请求  加requestBody
    public Result register(@RequestBody RegisterVo registerVo, HttpServletRequest request) {
        String phone = registerVo.getPhone();
        String nickName = registerVo.getNickName();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //判断表单项是否为空
        if (StringUtil.isEmpty(nickName) || StringUtil.isEmpty(code) || StringUtil.isEmpty(password) || StringUtil.isEmpty(phone)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        // 判断验证码是否正确
        String codeOk = (String) request.getSession().getAttribute("CODE");

        if (!code.equals(codeOk)) {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }

        //判断手机是否被注册
        UserInfo userInfo = userInfoService.getByPhone(phone);

        if (userInfo != null) {

            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setNickName(nickName);
        userInfo.setStatus(1);
        //密码进行密文加密
        userInfo.setPassword(MD5.encrypt(password));

        //  调用业务层完成注册功能
        userInfoService.insert(userInfo);

        //采取的不是thyleaf  所以在 sessIon中拿不到数据
        //登录的时候  注册不需要返回数据
        return Result.ok();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
        //非空判断
        if (StringUtil.isEmpty(password) || StringUtil.isEmpty(phone)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }

        //根据电话 获取用户信息
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (userInfo == null) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }

        //判断密码是否正确
        if (!userInfo.getPassword().equals(MD5.encrypt(password))) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }

        //根据状态码 判断用户是否被锁定
        if (userInfo.getStatus() != 1) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
            //该账户已经被锁定
        }

        request.getSession().setAttribute("USER", userInfo);
        //已经把用户信息传递给客户端保存，为什么还要在服务器的session中保存信息


        //登录成功   通过axaj将用户的某些信息返回 在回调函数进行处理  存储道客户端本地
        Map map = new HashMap();
        map.put("nickName", userInfo.getNickName());  //真正用到的是nickName
        map.put("phone", userInfo.getPhone());
        return Result.ok(map);


    }

    //注销功能
    @RequestMapping("/logout")
    public Result logout(HttpServletRequest request) {
        request.getSession().invalidate();  //不仅仅 remove了attribute("USER")也清空了 session里面的其他信息
        //清空 session
        return  Result.ok();  //跳过去之后会重新开始一个新的session对话

    }


}
